import { createWebHistory, createRouter, RouteRecordRaw } from "vue-router";

import { getStore } from "@/store";

const routes: RouteRecordRaw[] = [
    {
        path: "/",
        name: "layout",
        redirect: "/dashboard",
        component: () => import("@/layout/layout.vue"),
        children: [
            {
                path: "/dashboard",
                name: "dashboard",
                component: () => import("@/views/dashboard/index.vue"),
                meta: { title: "首页" },
            },
            {
                path: "/user",
                name: "user",
                redirect: "/user/customer",
                children: [
                    {
                        path: "/user/customer",
                        name: "customer",
                        component: () =>
                            import("@/views/user/customer/index.vue"),
                        meta: { title: "用户列表" },
                    },
                    {
                        path: "/user/admin",
                        name: "admin",
                        component: () => import("@/views/user/admin/index.vue"),
                        meta: { title: "管理员列表", role: 2 },
                    },
                ],
                meta: { title: "角色管理" },
            },
            {
                path: "/intro",
                name: "intro",
                redirect: "/intro/model",
                children: [
                    {
                        path: "/intro/model",
                        name: "modelIntro",
                        component: () =>
                            import("@/views/intro/model/index.vue"),
                        meta: { title: "模型介绍" },
                    },
                    {
                        path: "/intro/other",
                        name: "otherIntro",
                        component: () =>
                            import("@/views/intro/other/index.vue"),
                        meta: { title: "其他介绍" },
                    },
                ],
                meta: { title: "知识介绍" },
            },
            {
                path: "/predict",
                name: "predict",
                redirect: "/predict/module",
                children: [
                    {
                        path: "/predict/module",
                        name: "modulePredict",
                        component: () =>
                            import("@/views/predict/module/index.vue"),
                        meta: { title: "模型预测" },
                    },
                    {
                        path: "/predict/user",
                        name: "userPredict",
                        component: () =>
                            import("@/views/predict/user/index.vue"),
                        meta: { title: "用户预测" },
                    },
                ],
                meta: { title: "预测数据" },
            },
            {
                path: "/other",
                name: "other",
                redirect: "/other/problem",
                children: [
                    {
                        path: "/other/problem",
                        name: "problem",
                        component: () =>
                            import("@/views/other/problem/index.vue"),
                        meta: { title: "问题反馈" },
                    },
                    {
                        path: "/profile",
                        name: "profile",
                        component: () =>
                            import("@/views/other/profile/index.vue"),
                        meta: { title: "个人中心" },
                    },
                ],
                meta: { title: "其他功能" },
            },
        ],
    },
    {
        path: "/login",
        name: "login",
        component: () => import("@/views/login/index.vue"),
    },
    {
        path: "/forget",
        name: "forget",
        component: () => import("@/views/login/forget.vue"),
    },
    {
        path: "/:pathMatch(.*)*",
        name: "404",
        component: () => import("@/views/error/404.vue"),
        props: (route) => ({ from: route.query.from }), // 接收来源页面参数
    },
];

const router = createRouter({
    history: createWebHistory(),
    routes,
});

router.beforeEach((to, from, next) => {
    window.NLoadingBar.start();

    const useStore = getStore("user");
    if (
        useStore.getIsAuthenticated ||
        to.path === "/login" ||
        to.path === "/forget"
    ) {
        if (to.matched.length === 0) {
            next("/404"); // 无匹配路由
        } else {
            next();
        }
    } else {
        next("/login"); // 未登录，跳转到登录页面
    }
});

router.afterEach(() => {
    window.NLoadingBar.finish();
});

export default router;
